package com.systop.asset.service;

import com.systop.asset.entity.LoginBody;
import com.systop.asset.entity.User;
import com.systop.asset.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;
    public long login (LoginBody loginBody) {
        long userid = -1;
        User user = userMapper.selectUserByUsername(loginBody.getUsername());
        if (user != null) {

            System.out.println("raw=" + loginBody.getPassword());
            System.out.println("encode=" + user.getPassword());
            System.out.println("and ======== " + passwordEncoder.matches(loginBody.getPassword(), user.getPassword()));

            if (passwordEncoder.matches(loginBody.getPassword(), user.getPassword())) {
                userid = user.getId();
            };

        }
        return userid;
    }

}
